package com.priox.sigar.util;



import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * User: Matrix
 * Date: 26/01/13
 */
public class NumberUtil {


    public static DecimalFormat buildDecimalFormat() {
        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(new Locale("es"));
        symbols.setDecimalSeparator('.');
        symbols.setGroupingSeparator(',');
        return new DecimalFormat("#,###,###,##0.00", symbols);
    }

    public static void main(String[] args) {
        System.out.println(NumberUtil.getNumber("25.3"));
        System.out.println(NumberUtil.getNumber("25,3"));
        System.out.println(NumberUtil.getNumber("2,554.3"));
        System.out.println(NumberUtil.getNumber("2554"));
    }

    public static BigDecimal getNumber(String value) {
        if(value.contains(",") && value.contains(".")){
            return new BigDecimal(value.replace(",",""));

        }else if (value.contains(",")){
            String[] token= StringUtils.split(value,",");
            int i=1;
            String result="";
            for (String aToken : token) {
                 if(token.length==i){
                     result+= StringUtils.rightPad(aToken,3,'0');
                 }else{
                     result+=aToken;
                 }
                i++;
            }
            return new BigDecimal(result);
        }
        return new BigDecimal(value);
    }

    public static BigDecimal nvlIsZero(BigDecimal value1) {
        if(value1==null){
            return BigDecimal.ZERO;
        }
        return value1;
    }
}
